import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Z.YD
 * Date: 2025-07-28
 * Time: 21:35
 */
public class Mystack {
    int[] array;
    int size;

    public Mystack() {
        array = new int[3];
    }

    public int push(int e) {
        ensureCapacity();//扩容
        array[size++] = e;
        return e;
    }

    private void ensureCapacity() {
        if(size == array.length) {
            array = Arrays.copyOf(array,size*2);
        }
    }

    public int pop() {
        int e = peek();
        size--;
        return e;
    }

    public int peek() {
        if(empty()) {
            throw new RuntimeException("栈为空，无法获取栈顶元素");
        }
        return array[size-1];
    }

    public int size() {
        return size;
    }

    private boolean empty() {
        return 0 == size;
    }
}
